package com.nakamachizu.controller;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.LinkedHashMap;
import java.util.List;
import java.util.Map;

import org.slim3.controller.Controller;
import org.slim3.controller.Navigation;
import org.slim3.datastore.Datastore;
import org.slim3.util.StringUtil;

import com.google.appengine.api.users.User;
import com.google.appengine.api.users.UserService;
import com.google.appengine.api.users.UserServiceFactory;
import com.nakamachizu.meta.RegisterdMemberDataMeta;
import com.nakamachizu.model.RegisterdMemberData;
import com.nakamachizu.util.ForwardUtil;

public class MemberListController extends Controller {

    private UserService userService = UserServiceFactory.getUserService();
    
    private RegisterdMemberDataMeta meta = RegisterdMemberDataMeta.get();
    
    private String selected;
    
    private Map<String, String>selectedMap;
    
    
    @Override
    public Navigation run() {
        
        init();
        
        User user = userService.getCurrentUser();
        
        if(user == null){
            return redirect(userService.createLoginURL(request.getRequestURI()));
        }
        
        List<RegisterdMemberData>list = Datastore.query(meta)
                                        .filter(meta.adminMailAddress.equal(user.getEmail()))
                                        .sort(meta.name.asc)
                                        .asList();
        
        requestScope("memberList", this.removeSelected(list));
        
        return forward(ForwardUtil.forwardJsp("memberList.jsp"));
    }
    
    protected void init(){
        selected = requestScope("selected");
        if(!StringUtil.isEmpty(selected)){
            selected.replaceAll(",$", "");
            String[] array = selected.split(",");
            if(array != null){
                selectedMap = new HashMap<String, String>();
                for(String str : array){
                    selectedMap.put(str, "1");
                }
            }
        }
    }
    
    protected List<RegisterdMemberData> removeSelected(List<RegisterdMemberData>list){
        if(list != null && selectedMap != null){
            List<RegisterdMemberData>removeList = new ArrayList<RegisterdMemberData>();
            for(RegisterdMemberData data :list){
                if(!StringUtil.isEmpty(selectedMap.get(data.getMailAddress()))){
                    removeList.add(data);
                }
            }
            if(removeList.size() > 0){
                list.removeAll(removeList);
            }
        }
        return list;
    }
    
    protected List<String>margeAddress(List<String> list){
        List<String>result = null;
        if(list != null){
            Map<String, String>map = new LinkedHashMap<String, String>();
            result = new ArrayList<String>();
            for(String str : list){
                map.put(str, "1");
            }
            for(String str : map.keySet()){
                result.add(str);
            }
            
        }
        return result;
    }
}
